package pt.ua.helpdesk.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;


/**
 * Class responsible for removing a ticket
 * 
 * @author Amigos do Noddy
 *
 */
public class TicketRemove extends Composite {

	private Integer IDTicket;
		
	/**
	 * Constructor
	 * 
	 * @param idTicket ID of the ticket to be removed
	 */
	public TicketRemove(Integer idTicket) {

		this.IDTicket = idTicket;
		
		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel.add(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_3);
		horizontalPanel_3.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final Label lblperg = new Label("Tem a certeza que pertende remover?");
		horizontalPanel_3.add(lblperg);
		lblperg.setStyleName("label");

		final HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_4);
		horizontalPanel_4.setSpacing(10);

		final Button btnYes = new Button();
		horizontalPanel_4.add(btnYes);
		
		btnYes.setStyleName("button");
		btnYes.setText("Sim");

		final Button btnNo = new Button();
		horizontalPanel_4.add(btnNo);
		btnNo.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				if(Default.getSession("Tipo").toString().equals("2"))
				{	
					Default.loadComposite(new TicketsView());
				}
				else
				{
					Default.loadComposite(new TicketsViewPending());
				}
			}
		});
		btnNo.setStyleName("button");
		btnNo.setText("Não");

		

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final Label lblAlerta = new Label("");
		horizontalPanel_1.add(lblAlerta);
		lblAlerta.setStyleName("label");

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);

		final Button btnBack = new Button();
		horizontalPanel_2.add(btnBack);
		btnBack.setVisible(false);
		btnBack.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) 
			{
				if(Default.getSession("Tipo").toString().equals("2"))
				{	
					Default.loadComposite(new TicketsView());
				}
				else
				{
					Default.loadComposite(new TicketsViewPending());
				}
			}
		});
		btnBack.setStyleName("button");
		btnBack.setText("Voltar");
		
		btnYes.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				btnYes.setVisible(false);
				btnNo.setVisible(false);
				lblperg.setVisible(false);
				
				btnBack.setVisible(true);
				ServiceTickets.Util.getInstance().removeTicket(IDTicket, new RemoveTicketAsyncCallback(lblAlerta));
			}
		});
	}
	
	/**
	 * AsyncCallback responsible for displaying feedback on whether the ticket was successfully removed or not
	 * 
	 * @author Amigos do Noddy
	 *
	 */
	static class RemoveTicketAsyncCallback implements AsyncCallback {
		  Label label;
		  
		  /**
		   * Constructor
		   * 
		   * @param label Label where feedback is displayed
		   */
		  public RemoveTicketAsyncCallback(Label label) {
			  this.label= label;
		  }
		  
		  public void onSuccess(Object object) {
			  Boolean OK = (Boolean)object;
			  String message;
	    	  if(OK.booleanValue())
	    	  {
	    		  message = "Ticket removido com sucesso";
	    		  DOM.setInnerHTML(label.getElement(), message);
	    	  }
	    	  else
	    	  {
	    		  message = "Ocorreu um erro inesperado";
	    		  DOM.setInnerHTML(label.getElement(), message);
	    	  }
		  }
		  public void onFailure(Throwable throwable) {
			  System.out.println("FALHOU!");
		  }
		}

}
